﻿Imports System.Reflection

Public Class FrmMenu

    Public Shared ban As Boolean

    Public Sub New(CodUsuario As String, CodModulo As String, vigencia As String)
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        If CodUsuario Is Nothing Then
            Throw New ArgumentNullException()
        End If
        If CodModulo Is Nothing Then
            Throw New ArgumentNullException()
        End If
        cargarMenu(CodUsuario, CodModulo, vigencia)
    End Sub

    Sub cargarMenu(CodUsuario As String, CodModulo As String, vigencia As String)
        msMenu.Items.Clear() ' borro items del menu
        ssMenuEstado.Items.Clear() ''borro los items de la barra de estado

        Dim row As DataRow
        Dim dtUsuario As DataTable = GBDZafiro.Usuario.ConsxCodUsuario(CodUsuario)
        row = dtUsuario.Rows(0)

        Dim dtMenu As DataTable = GBDZafiro.Menu.ConsxModuloPerfil(CodModulo, row.Item(2).ToString())

        If dtMenu.Rows.Count <= 0 Then
            MessageBox.Show("El usario no tiene acceso al modulo sele " + CodModulo, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        Dim mnuOpcion As ToolStripMenuItem

        Dim drMenu As DataRow
        Dim i = dtMenu.Rows.Count
        While i > 0
            drMenu = dtMenu.Rows.Item(i - 1)
            If DBNull.Value.Equals(drMenu("PADRE")) Then

                mnuOpcion = New ToolStripMenuItem(drMenu("TITULO").ToString())
                mnuOpcion.Tag = drMenu("ID_MENU").ToString()
                ' añado al menu
                msMenu.Items.Add(mnuOpcion)
                'FrmMenu.msMenu.Items.Add(drMenu("TITULO"))
                'subopciones
                crearHojas(mnuOpcion, dtMenu, drMenu("COD_MENU").ToString())
            End If
            i = i - 1
        End While

        addMenuEstado(row)
        ' Me.Visible = False
        'Me.Close()
    End Sub

    Private Sub addMenuEstado(ByRef row As DataRow)
        '''''' Agrego el estado al menu estado
        'Dim estado As ToolStripItem=FrmMenu.ssMenuEstado.n
        Dim estado As New ToolStripMenuItem("Usuario: ")
        ssMenuEstado.Items.Add(estado)
        estado = New ToolStripMenuItem(row("COD_USUARIO").ToString())
        estado.Tag = "usuario"
        ssMenuEstado.Items.Add(estado)
        estado = New ToolStripMenuItem("Año Fiscal: ")
        ssMenuEstado.Items.Add(estado)
        estado = New ToolStripMenuItem()
        estado.Tag = "añofiscal"
        ssMenuEstado.Items.Add(estado)
        ssMenuEstado.Enabled = False
    End Sub

    Sub crearHojas(ByRef mnuOpcion As ToolStripMenuItem, ByVal dtMenu As DataTable, cod_menu As String)
        Dim mnuSubOpcion As ToolStripMenuItem
        For Each drMenu As DataRow In dtMenu.Rows
            If drMenu("PADRE").Equals(cod_menu) And _
                Not drMenu("COD_MENU").Equals(drMenu("PADRE")) Then

                mnuSubOpcion = New ToolStripMenuItem(drMenu("TITULO").ToString())
                mnuSubOpcion.Tag = drMenu("FRM").ToString()
                AddHandler mnuSubOpcion.Click, AddressOf itemClicked

                mnuOpcion.DropDownItems.Add(mnuSubOpcion)
                crearHojas(mnuSubOpcion, dtMenu, drMenu("COD_MENU"))
            End If
        Next
    End Sub

    Private Sub itemClicked(ByVal sender As Object, ByVal e As EventArgs)
        'ToolStripMenuItem clickedItem = (ToolStripMenuItem)sender;
        Dim clickedItem As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
        Dim opcion As String = clickedItem.Tag.ToString()
        Dim nomOpcion As String = clickedItem.Text

        If Not opcion.Length < 1 Then
            Dim frm As New Form
            'Dim ns = obj.[GetType]().[Namespace]
            'MessageBox.Show(ns.ToString())
            Dim o As New Object

            opcion = [Assembly].GetEntryAssembly.GetName.Name & "." & opcion
            o = [Assembly].GetEntryAssembly.CreateInstance(opcion)

            frm = CType(o, Form)
            frm.MdiParent = Me
            frm.Show()

        ElseIf StrComp(nomOpcion, "Cerrar Sesion") = 0 Then
            Me.Close()
        End If

    End Sub

    Private Sub FrmMenu_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'SetMdiContainer(Me, Color.AliceBlue)
        fondoMenu()
    End Sub

    Private Sub FrmMenu_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        FrmLogin.Close()
        If MessageBox.Show("¿Esta seguro de salir de Zafiro?", "Zafiro", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            e.Cancel = True
            ban = True
        Else
            ban = False
        End If
    End Sub

    Sub fondoMenu()
        Dim ctl As Control
        Dim ctlMDI As MdiClient
        For Each ctl In Me.Controls
            Try
                ctlMDI = CType(ctl, MdiClient)
                ctlMDI.BackColor = Color.White
            Catch ex As InvalidCastException

            End Try
        Next
    End Sub

    Private Sub FrmMenu_FormClosed(sender As System.Object, e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed

    End Sub

    Private Sub tsbcerrarsesion_Click(sender As System.Object, e As System.EventArgs) Handles tsbcerrarsesion.Click
        Me.Close()
    End Sub

    Private Sub logOut()
        FrmLogin.Close()
        If MessageBox.Show("¿Esta seguro de salir de Zafiro?", "Zafiro", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Me.Close()
        End If
    End Sub

    Private Sub tsbCambiarContrasena_Click(sender As System.Object, e As System.EventArgs) Handles tsbCambiarContrasena.Click
        showCambiarContraseña()
    End Sub

    Sub showCambiarContraseña()
        FrmCambiarContrasena.MdiParent = Me
        FrmCambiarContrasena.Show()
    End Sub

    Private Sub tsbUsuarios_Click(sender As System.Object, e As System.EventArgs) Handles tsbUsuarios.Click
        showfrmUsuarios()
    End Sub

    Private Sub showfrmUsuarios()
        FrmUsuario.MdiParent = Me
        FrmUsuario.Show()
    End Sub

    Private Sub tsbBloquear_Click(sender As System.Object, e As System.EventArgs) Handles tsbBloquear.Click
        Me.tsBarra.Enabled = False
        Me.msMenu.Enabled = False
        Me.ControlBox = False
        'FrmBloquearEscritorio.MdiParent = Me
        FrmBloquearEscritorio.ShowDialog(Me)
        Me.tsBarra.Enabled = True
        Me.msMenu.Enabled = True
        Me.ControlBox = True
    End Sub

    Private Sub tsBarra_ItemClicked(sender As System.Object, e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles tsBarra.ItemClicked

    End Sub

End Class